package com.xxx.filedownload;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class FileCilent {

	public static void main(String[] args) {

		Socket socket = null;
		OutputStream outputStream = null;
		InputStream inputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			socket = new Socket("127.0.0.1" , 9999);
			outputStream = socket.getOutputStream();
			String fileName = "97442170_p0_master1200.jpg";
			outputStream.write(fileName.getBytes());
			outputStream.flush();


			//获取文件
			inputStream = socket.getInputStream();
			fileOutputStream = new FileOutputStream("E:\\中转\\" + fileName);
			byte[] b = new byte[1024];
			int len;
			while ((len = inputStream.read(b)) != -1) {

				fileOutputStream.write(b);
				fileOutputStream.flush();

			}


		} catch (IOException e) {
			e.printStackTrace();
		}finally {

			try {

				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
				if (inputStream != null) {
					inputStream.close();
				}
				if (outputStream != null) {
					outputStream.close();
				}
				if (socket != null) {
					socket.close();
				}


			} catch (IOException e) {
				e.printStackTrace();
			}
		}



	}
}
